package com.gjintel.framework.validateunique.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.gjintel.framework.validateunique.dao.IValidateUniqueDao;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.util.StringUtil;

/**
 * @description 知识点对应的数据访问的实现
 * @author fengya
 * @date 2015-5-26 下午1:35:14
 */
@Repository("validateUniqueDao")
public class ValidateUniqueDaoImpl extends HibernateGenericDAO implements IValidateUniqueDao {

	@SuppressWarnings("unchecked")
	@Override
	public boolean unique(String tableName, String field, String value, String id) {
		String sql;
		List<String> params = new ArrayList<String>();
		if (StringUtil.isEmpty(id)) {
			sql = "select id from " + tableName + " where " + field + "=?";
			params.add(value);
		} else {
			sql = "select id from " + tableName + " where " + field + "=? and id!=? ";
			params.add(value);
			params.add(id);
		}
		List<Object> list = this.findBySql(sql, params.toArray());
		if (list == null || list.isEmpty()) {
			return true;
		}
		return false;
	}

}
